import tornado.ioloop
import tornado.httpserver
import tornado.options
import session
from urls import handlers

# 定义运行参数
tornado.options.define('port', default=8000, type=int)

class Application(tornado.web.Application):
    def __init__(self):
        settings = dict(
            debug=True,
            cookie_secret=b"e446976944b4e8442f099fed1f3fea48462e5832f483a0ed9a3d5d3859f==78d",
            session_secret=b"3c5cb1f00803e6e78ab50b466a40b9977dbe96840c38307f428b25e2277f1bcc",
            # 半小时过期
            session_timeout=1800,
            store_options={
                'redis_host': 'localhost',
                'redis_port': 6379,
                'redis_pass': '',
            },
            template_path="view",
            # static_path='static'
        )
        tornado.web.Application.__init__(self, handlers, **settings)
        # 单实例session_manager
        self.session_manager = session.SessionManager(settings["session_secret"], settings["store_options"],
                                                      settings["session_timeout"])


if __name__ == '__main__':
    # 从 配置文件导入
    tornado.options.options.parse_command_line()

    app = Application()
    httpServer = tornado.httpserver.HTTPServer(app)

    # 使用变量的值
    httpServer.bind(tornado.options.options.port)
    print("server run on port:{}".format(tornado.options.options.port))
    httpServer.start(1)

    tornado.ioloop.IOLoop.current().start()
